package gui.mvc;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * @author Ann Raboschuk
 */
public class PersonDialog extends JDialog
{
   private static final long serialVersionUID = 1L;
   private Component[] controls;
   private String[] person;
   
   public PersonDialog()
   {
      this.setContentPane(createControlPanel());
//      Session session = null;
//      try
//      {
//         session = HibernateUtil.getSessionFactory().openSession();
//         //Long lastID = (Long)session.createQuery(GET_MAX_ID).uniqueResult();
//         Long lastID = (Long)session.createQuery("select max(id) from api.Person").uniqueResult();
//         ((JTextField) controls[0]).setText(lastID+1+"");
//      }
//      catch (Exception e)
//      {
//         e.printStackTrace();
//      }
//      finally
//      {
//         if (session != null && session.isOpen())
//         {
//            session.close();
//         }
//      }
      this.setTitle("Add person");
   }
   
   public PersonDialog(String[] fields)
   {
      this.setContentPane(createControlPanel());
      this.setTitle("Edit person");
      for (int i=0; i<fields.length; i++)
      {
         if (i==4)
            ((JComboBox) controls[i]).setSelectedIndex(fields[4].equals("F") ? 0 : 1);
         else
            ((JTextField) controls[i]).setText(fields[i]);
      }
   }
   
   public String[] showDialog()
   {
      this.pack();
      this.setModal(true);
      this.setResizable(false);
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      this.setLocation((screenSize.width - this.getWidth()) / 2,
            (screenSize.height - this.getHeight()) / 2);
      this.setVisible(true);
      return person;
   }
   
   private JPanel createControlPanel()
   {
      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(0,2));
      String[] labels = {"ID", "Surname", "Name", "Age", "Sex", "E-mail", "Phone", "Address"};
      controls = new Component[labels.length];
      
      for (int i=0; i<controls.length; i++)
      {
         if (i==4)
            controls[i] = new JComboBox(new String[]{"F","M"});
         else
         {
            controls[i] = new JTextField(15);
            if (i==0)
               controls[0].setFocusable(false);
         }
         panel.add(new JLabel(labels[i]));
         panel.add(controls[i]);
      }
      
      ActionListener btnlistener = new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent event)
         {
            if (event.getActionCommand().equals("OK"))
            {
               person = new String[controls.length];
               for (int i=0; i<person.length; i++)
               {
                  if (i==4)
                     person[i] = ((JComboBox) controls[i]).getSelectedItem().toString();
                  else
                     person[i] = ((JTextField) controls[i]).getText();
               }
            }
            else 
               person = null;
            setVisible(false);
            dispose();
         }
      };
      String[] buttons = {"OK", "Cancel"};
      for (int i=0; i<buttons.length; i++)
      {
         JButton button = new JButton(buttons[i]);
         button.addActionListener(btnlistener);
         panel.add(button);
      }      
      return panel;
   }
}
